package com.zefor.task;

import com.zefor.pojo.ElectricityPojo;
import com.zefor.pojo.WaterPojo;
import com.zefor.utils.AdmqUtil;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AccessWaterTask extends AbstractTask {

    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        DynamicObject[] farms = BusinessDataServiceHelper.load("n716_farm", "id,name", null);
        ArrayList<WaterPojo> water = (ArrayList<WaterPojo>) AdmqUtil.getWater();
        int index = 0;
        for (DynamicObject farm : farms) {
            //查询昨日当前养殖场用水量详情
            DynamicObject[] oldEleInfo = BusinessDataServiceHelper.load("n716_water_info", "id,n716_tmds", new QFilter("n716_basedatafield.id", QCP.equals, farm.get("id")).toArray(), "createtime desc", 1);
            //获取当前养殖场下的所有栏舍
            DynamicObject[] corrals = BusinessDataServiceHelper.load("n716_corral", "id,name,n716_farm_basedata", new QFilter("n716_farm_basedata.id", QCP.equals, farm.get("id")).toArray());
            //创建用水记录单据对象
            DynamicObject waterInfo = BusinessDataServiceHelper.newDynamicObject("n716_water_info");
            //昨日养殖场分录数据
            DynamicObjectCollection old = new DynamicObjectCollection();
            //是否存在老的用水数据
            if (oldEleInfo.length > 0) {
                old = BusinessDataServiceHelper.loadSingle(oldEleInfo[0].getString("id"), "n716_water_info").getDynamicObjectCollection("n716_entryentity");
            }
            //获取单据分页对象列表
            DynamicObjectCollection list = waterInfo.getDynamicObjectCollection("n716_entryentity");

            //今日用水数据
            double tmsSum = 0D;

            for (int i = 0; i < corrals.length; i++) {
                DynamicObject temp = new DynamicObject(list.getDynamicObjectType());
                temp.set("n716_basedatafield1", corrals[i]);
                //如果出现新增的情况,那么新增的列初始值设置为0
                if (i < old.size()) {
                    //昨日用水数据
                    temp.set("n716_decimalfield1", old.get(i).get("n716_decimalfield2"));
                    //今日水表数据
                    temp.set("n716_decimalfield2", Double.parseDouble(old.get(i).getString("n716_decimalfield2")) + water.get(index).getWaterconsumption());
                } else {
                    //昨日水表数据
                    temp.set("n716_decimalfield1", 0);
                    //今日水表数据
                    temp.set("n716_decimalfield2", water.get(index).getWaterconsumption());
                }
                //今日用水量
                temp.set("n716_decimalfield3", water.get(index).getWaterconsumption());
                //记录今日用水量总和
                tmsSum += water.get(index).getWaterconsumption();
                //记录时间
                temp.set("n716_datefield1", water.get(index).getTimestamp().getTime() / 1000);
                list.add(temp);
                index++;
            }

            //设置养殖场信息
            waterInfo.set("n716_basedatafield", farm);
            //单据状态
            waterInfo.set("billstatus", "A");
            //设置当日养殖场用水量
            waterInfo.set("n716_decimalfield", tmsSum);
            //保存数据
            SaveServiceHelper.saveOperate("n716_water_info", new DynamicObject[]{waterInfo}, OperateOption.create());
        }
    }
}
